package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysMenuMapper;
import net.zigin.nhi.could.waste.dao.SysRoleMapper;
import net.zigin.nhi.could.waste.dao.SysUserHospitalMapper;
import net.zigin.nhi.could.waste.dto.SysRoleDto;
import net.zigin.nhi.could.waste.model.SysRole;
import net.zigin.nhi.could.waste.service.SysRoleService;
import net.zigin.nhi.could.waste.service.SysUserService;
import net.zigin.nhi.could.waste.vo.SysMenuVo;
import net.zigin.nhi.could.waste.vo.SysRoleVo;
import net.zigin.nhi.could.waste.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-7 19:08:55                                               
##################################################################*/


/**
 * SysRole服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysRoleServiceImpl
 * @date 2021-9-7 19:08:55
 */
@Service
public class SysRoleServiceImpl extends AbstractServiceImpl<SysRole, String> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;

    @Override
    public BaseMapper<SysRole, String> getMapper() {
        return sysRoleMapper;
    }

    @Override
    public List<SysRoleVo> infoByUserId(String userId) {
        return ReflectUtil.copyProperties(sysRoleMapper.infoByUserId(userId), SysRoleVo.class);
    }

    @Override
    public List<SysRoleVo> getRoleList(SysRoleDto roleDto) {
        Map<String, Object> paramMap = getRoleParams(roleDto);
        List<SysRole> roles;
        if (roleDto.getIsPage()) {
            roles = sysRoleMapper.selectListByPage(paramMap);
        } else {
            roles = sysRoleMapper.selectListByCondition(paramMap);
        }
        List<SysRoleVo> roleVos = ReflectUtil.copyProperties(roles, SysRoleVo.class);
        if (roleVos == null) {
            return new ArrayList<>();
        }
        return roleVos;
    }

    @Override
    public Integer getRoleCount(SysRoleDto roleDto) {
        Map<String, Object> paramMap = getRoleParams(roleDto);
        return sysRoleMapper.selectCount(paramMap);
    }

    private Map<String, Object> getRoleParams(SysRoleDto roleDto) {
        SysUserVo currentUser = sysUserService.getCurrentUser();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(currentUser.getId());
        if (StringUtil.isNullOrEmpty(roleDto.getSysFlag())) {
            roleDto.setSysFlag(currentUser.getType());
        }
        if (!StringUtil.isNullOrEmpty(roleDto.getDoublePlatformFlag())) {
            if (roleDto.getDoublePlatformFlag().equals("1")) {
                roleDto.setSysFlag("");
            } else {
                roleDto.setDoublePlatformFlag("");
            }
        }
        return ParamsMapUtil.getInstance()
                .addParam("hospitalBaseId", hospitalBaseId)
                .addParam("doublePlatformFlag", roleDto.getDoublePlatformFlag())
                .addParam("sysFlag", roleDto.getSysFlag())
                .addParam("code", roleDto.getCode())
                .addParam("workGroup", roleDto.getWorkGroup())
                .addParam("sysFlag", roleDto.getSysFlag())
                .addParam("name", roleDto.getName())
                .addParam("pageIndex", roleDto.getPageIndex())
                .addParam("pageSize", roleDto.getPageSize());
    }

}